<?php
/**
 * Created By: ST2
 * Created on 2015-11-04 17:49:30
 * coding: utf-8
*/

class shmCache {
	protected $shm_id = 0;
	protected $shm_key = '';

	public function __construct($key = null) {
		if ( empty($key) ) {
			// $tmp = tempnam('/tmp', 'PHP');
			$this->shm_key = ftok(__FILE__, 'a');
			// echo $this->shm_key;
			$this->shm_id = shm_attach($this->shm_key, 33554432);
		} else {
			$this->shm_key = $key;
			// $this->shm_id = sem_get($this->shm_key);
			$this->shm_id = shm_attach($this->shm_key, 33554432);
		}

		if(!$this->shm_id) {
            exit('Unable to create shared memory segment');
		}
	}

	public function get($key) {
		if ($this->has($key)) {
			return shm_get_var($this->shm_id, $this->shm_key($key));
		}
		return false;
	}

	public function del($key) {
		if ( $this->has($key) ) {
			return shm_remove_var($this->shm_id, $this->shm_key($key));
		}
	}
	public function save($key, $val) {
		return shm_put_var($this->shm_id, $this->shm_key($key), $val);
	}
	
	private function has($key) {
		if (shm_has_var($this->shm_id, $this->shm_key($key))) {
			return true;
		}
		return false;
	}

	public function shm_key($val){ // enable all world langs and chars !
	    return preg_replace("/[^0-9]/","",(preg_replace("/[^0-9]/","",md5($val))/35676248)/619876);
	 }

	public function flush() {
		return shm_remove($this->shm_id);
	}
	
	public function __destruct() {
		shm_detach($this->shm_id);
		unset($this);
	}
}


$shmCache = new shmCache();
$shmCache->save('a', 'hello world!');

echo $shmCache->get('a');


// $shx= new shmSmart;
// $shx->put("key_name_apple","key_val_peach"); //set example..
// $shx->put("key name alternative array",array(1=>"banana","apricot","blablabla"=>array("new-blaala"))); //set array example..
// echo  $shx->get("key_name_apple"); // get example key value.
// $shx->del("key_name_apple"); // delete key
// unset($shx); // free memory in php..

// class shmSmart{
//   public $shm;            //holds shared memory resource
//   public function __construct(){
//     if(function_exists("shm_attach")===FALSE){
//       die("\nYour PHP configuration needs adjustment. See: http://us2.php.net/manual/en/shmop.setup.php. To enable the System V shared memory support compile PHP with the option --enable-sysvshm.");
//     }  
//     $this->attach();    //create resources (shared memory)
//   }
//   public function attach(){
//     $this->shm=shm_attach(0x701da13b,33554432);    //allocate shared memory
//   }
//   public function dettach(){
//     return shm_detach($this->shm);    //allocate shared memory
//   }
//   public function remove(){
//     return shm_remove($this->shm);    //dallocate shared memory
//   }
//   public function put($key,$var) {
//     return shm_put_var($this->shm,$this->shm_key($key),$var);    //store var 
//   }
//   public function get($key){
//     if($this->has($key)){
//       return shm_get_var($this->shm,$this->shm_key($key));  //get var
//     }else{
//       return false;    
//     }        
//   }
//   public function del($key){
//     if($this->has($key)){
//       return shm_remove_var($this->shm,$this->shm_key($key)); // delete var
//     }else{
//       return false;    
//     }        
//   }
//   public function has($key){
//     if(shm_has_var($this->shm,$this->shm_key($key))){ // check is isset
//       return true;        
//     }else{
//       return false;        
//     }
//   }
//   public function shm_key($val){ // enable all world langs and chars !
//     return preg_replace("/[^0-9]/","",(preg_replace("/[^0-9]/","",md5($val))/35676248)/619876); // text to number system.
//   }
//   public function __wakeup() {
//     $this->attach();
//   }
//   public function __destruct() {
//     $this->dettach();
//     unset($this);
//   }    
// }